//
//  RHHomeTopoHEBATView.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/4/25.
//

#import "RHHomeTopoHEBATView.h"
#import "YYAnimatedImageView.h"
#import "YYImage.h"

#define line_Width 1
@interface RHHomeTopoHEBATView()
@property (nonatomic, strong) RHTopoModel *topoModel;

@property (nonatomic, strong) UIImageView *topoImg;

@property (nonatomic ,strong) UIView *feed_indicator;

@property (nonatomic ,strong) UIView *battery_indicator;

@property (nonatomic ,strong) UIView *power_indicator;

@property (nonatomic ,strong) UIView *load_indicator;

@property (nonatomic ,strong) UIView *solar_indicator;

@property (nonatomic ,assign) BOOL isLandscape;
@end
@implementation RHHomeTopoHEBATView


-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = [UIColor whiteColor];
        
        [self setupUI];
    }
    return self;
}

-(instancetype)initWithOrientation:(BOOL)isLandscape
{
    if(self = [self initWithFrame:CGRectZero]){
        self.backgroundColor = [UIColor whiteColor];
        self.isLandscape = isLandscape;
        [self setupUI];
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    [self setNeedsDisplay];
}


-(void)setupUI
{
    kWeakSelf;
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(0);
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.bottom.mas_offset(0);
    }];
    
    
    _topoImg = [[UIImageView alloc] init];
    [bgView addSubview:_topoImg];
    
    
    //馈网
    _feed_indicator = [self indicatorViewWithTitle:@"电网".rh_localized powerUnit:@"kW" volUnit:@"V:--V/--V" Alignment:NSTextAlignmentRight];
    [bgView addSubview:_feed_indicator];
    
    
    //电池
    _battery_indicator = [self indicatorViewWithTitle:@"电池".rh_localized powerUnit:@"kW" volUnit:@"V:--V" Alignment:NSTextAlignmentRight];
    [bgView addSubview:_battery_indicator];
    
    
    //能量
    _power_indicator = [self indicatorViewWithTitle:@"能量".rh_localized powerUnit:@"%" volUnit:@"V:--V/--V" Alignment:NSTextAlignmentRight];
    [bgView addSubview:_power_indicator];
    
    
    //负载
    _load_indicator = [self indicatorViewWithTitle:@"负载".rh_localized powerUnit:@"kW" volUnit:@"V:--V/--V" Alignment:NSTextAlignmentLeft];
    [bgView addSubview:_load_indicator];
    
    
    //光伏
    _solar_indicator = [self indicatorViewWithTitle:@"光伏".rh_localized powerUnit:@"kW" volUnit:@"V:--V/--V" Alignment:NSTextAlignmentRight];
    [bgView addSubview:_solar_indicator];
    
    //
    [_topoImg setImage:[UIImage imageNamed:@"topo_he_bat_high"]];
    [_topoImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(20);
        make.bottom.mas_offset(-20);
        make.centerX.mas_offset(kScaleW(30));
        make.height.equalTo(weakSelf.topoImg.mas_width).multipliedBy(541/648.0);
    }];
    
    //馈网
    [_feed_indicator mas_makeConstraints:^(MASConstraintMaker *make) {
       
        if (weakSelf.isLandscape) {
            make.right.equalTo(weakSelf.topoImg.mas_left).offset(kScaleW(-15));
            make.centerY.equalTo(weakSelf.topoImg).offset(kScaleW(0));
        }else{
            make.right.equalTo(weakSelf.topoImg.mas_left).offset(kScaleW(-5));
            make.centerY.equalTo(weakSelf.topoImg).offset(kScaleW(-5));
        }
    }];
    
    //电池
    [_battery_indicator mas_makeConstraints:^(MASConstraintMaker *make) {
        if (weakSelf.isLandscape) {
            make.centerY.equalTo(weakSelf.topoImg.mas_bottom).offset(kScaleW(-50));
            make.right.equalTo(weakSelf.topoImg.mas_left).offset(kScaleW(-15));
        }else{
            make.centerY.equalTo(weakSelf.topoImg.mas_bottom).offset(kScaleW(-40));
            make.right.equalTo(weakSelf.topoImg.mas_left).offset(kScaleW(-5));
        }
        
    }];
    
    
    //负载
    [_load_indicator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.topoImg.mas_centerX).offset(kScaleW(weakSelf.isLandscape?80:55));
        make.centerY.equalTo(weakSelf.topoImg.mas_bottom).offset(-kScaleW(10));
    }];
    
    //光伏
    [_solar_indicator mas_makeConstraints:^(MASConstraintMaker *make) {
        if (weakSelf.isLandscape) {
            make.centerX.offset(kScaleW(-5));
            make.top.equalTo(weakSelf.topoImg).offset(kScaleW(40));
        }else{
            make.centerX.offset(kScaleW(-5));
            make.top.equalTo(weakSelf.topoImg).offset(kScaleW(10));
        }
        
    }];
    
    //能量
    [_power_indicator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.battery_indicator);
        make.width.offset(120);
        make.bottom.equalTo(weakSelf.feed_indicator.mas_top).offset(-kScaleW(20));
    }];
    _power_indicator.hidden = YES;
    
}




//首页进来     只有电池才除以1000
-(void)setTopoModel:(RHTopoModel *)topoModel deviceTypeCode:(NSString*)deviceTypeCode
{
    _topoModel = topoModel;
    //光伏功率
    QMUILabel *solarPowerLab = [self.solar_indicator viewWithTag:100];
    NSString *solar_content = [NSString stringWithFormat:@"%@kW",[NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[topoModel.pvPwr doubleValue]]];
    solarPowerLab.text = solar_content;

    
    
    //电网功率
    QMUILabel *gridLab = [self.feed_indicator viewWithTag:100];
    NSString *grid_content = [NSString stringWithFormat:@"%@kW",[NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[topoModel.gridPwr doubleValue]]];
    gridLab.text = grid_content;

    
    
    //电池功率
    QMUILabel *batteryLab = [self.battery_indicator viewWithTag:100];
    NSString *battery_content = [NSString stringWithFormat:@"%@kW",[NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[topoModel.batPwr doubleValue]]];
    batteryLab.text = battery_content;

    
    //负载功率
    QMUILabel *loadLab = [self.load_indicator viewWithTag:100];
    NSString *load_content = [NSString stringWithFormat:@"%@kW",[NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[topoModel.loadPwr doubleValue]]];
    loadLab.text = load_content;



}

-(NSAttributedString*)attrStringWithContent:(NSString *)content  attrKey:(NSString*)key
{
    NSDictionary *attiDic = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:10],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"3F4443" alpha:0.6]};
    
    NSMutableAttributedString *attri_content = [[NSMutableAttributedString alloc] initWithString:content];
    [attri_content setAttributes:attiDic range:[content rangeOfString:key]];
    return attri_content;
}


//家储示意图 (能量,馈网,电池,光伏,负载)
-(UIView*)indicatorViewWithTitle:(NSString*)title powerUnit:(NSString*)powUnit volUnit:(NSString*)volUnit Alignment:(NSTextAlignment)alignment;
{
    UIView *bgView = kView;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    bgView.backgroundColor = UIColor.clearColor;
    
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.textAlignment = alignment;
    titleLab.numberOfLines = 0;
    titleLab.text = title;
    titleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.6];
    titleLab.font = kSYSTEMFONT(12);
    [bgView addSubview:titleLab];
    
    //功率
    QMUILabel *contentLab = [[QMUILabel alloc]init];
    contentLab.textAlignment = alignment;
    contentLab.text = [NSString stringWithFormat:@"-- %@",powUnit];
    contentLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:1];
    contentLab.font = kSYSTEMFONT(10);
    contentLab.tag = 100;
    [bgView addSubview:contentLab];
    
    [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(titleLab);
        make.top.mas_offset(0);
        make.height.offset(15);
    }];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.top.equalTo(contentLab.mas_bottom).offset(2);
        make.bottom.mas_offset(0);
        
    }];
    
    return bgView;
}

@end
